package com.hand.hap.demo.service.impl;

import com.hand.hap.code.rule.exception.CodeRuleException;
import com.hand.hap.code.rule.service.ISysCodeRuleProcessService;
import com.hand.hap.core.IRequest;
import com.hand.hap.core.annotation.StdWho;
import com.hand.hap.system.dto.BaseDTO;
import com.hand.hap.system.dto.DTOStatus;
import com.hand.hap.system.service.IBaseService;
import com.hand.hap.system.service.impl.BaseServiceImpl;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hand.hap.demo.dto.DemoCompanyOrder;
import com.hand.hap.demo.service.IDemoCompanyOrderService;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service

public class DemoCompanyOrderServiceImpl extends BaseServiceImpl<DemoCompanyOrder> implements IDemoCompanyOrderService{

    @Autowired
    ISysCodeRuleProcessService codeRuleProcessService;

    @Autowired
    IDemoCompanyOrderService demoCompanyOrderService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public  List<DemoCompanyOrder> myBatchUpdate(IRequest request, @StdWho List<DemoCompanyOrder> list) throws CodeRuleException {

        IBaseService<DemoCompanyOrder> self = ((IBaseService<DemoCompanyOrder>) AopContext.currentProxy());
        for (DemoCompanyOrder t : list) {
            switch (((DemoCompanyOrder) t).get__status()) {

                case DTOStatus.ADD:
                    String company =  t.getOrderCompany();
                    Map<String, String> map = new HashMap<>();
                    map.put("var", company);
                    String m = codeRuleProcessService.getRuleCode("TASK_EXECUTION_ORDER", map);
                    t.setOrderCode(m);
                    self.insertSelective(request, t);
                    break;
                case DTOStatus.UPDATE:
                    if (useSelectiveUpdate()) {
                        self.updateByPrimaryKeySelective(request, t);
                    } else {
                        self.updateByPrimaryKey(request, t);
                    }
                    break;
                case DTOStatus.DELETE:
                    self.deleteByPrimaryKey(t);
                    break;
                default:
                    break;
            }
        }
        return list;
    }

    @Override
    public Integer batchDeleteByOrderId(DemoCompanyOrder dto) {
        int i = demoCompanyOrderService.deleteByPrimaryKey(dto);
        return i;
    }
}